import axios from "axios";

const service = axios.create({
  baseUrl: "",
  timeout: 60 * 1000,
});

// 请求拦截
service.interceptors.request.use(
  (config) => {
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截
service.interceptors.response.use((res) => {
  const { code, data, msg } = res.data;
  if (code === 200) {
    return data;
  } else {
    return Promise.reject(msg);
  }
});

// post 请求
export const post = (url, params, headers) => {
  return new Promise((resolve, reject) => {
    service
      .post(
        url,
        params,
        (headers = {
          ...headers,
        })
      )
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};

// get 请求
export const get = (url, params) => {
  return new Promise((resolve, reject) => {
    service
      .get(url, {
        params: params,
      })
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};
